//
//  SISSUploadHouseConditionInfoViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2019/7/4.
//  Copyright © 2019 优房网. All rights reserved.
//

#import "SISSUploadHouseConditionInfoViewController.h"
#import "SISSLabelTextFieldView.h"
#import "SISSUploadImageView.h"
#import "SISSUploadSuccessViewController.h"
#import "SISSNetworkingManager+HouseProgress.h"
#import "SISSHouseProgressModel.h"
#import "SISSRentOutProgressModel.h"
#import "SISSHouseManualModel.h"
#import "SISSNetworkingManager+RentOutProgress.h"

@interface SISSUploadHouseConditionInfoViewController ()

/// 房屋状况说明书电子档图片
@property (weak, nonatomic) SISSUploadImageView *uploadContractImageView;
/// 房源实景照片
@property (weak, nonatomic) SISSUploadImageView *uploadHouseImageView;
/// 备注
@property (weak, nonatomic) SISSLabelTextFieldView *remarkView;
/// tipLabel
@property (weak, nonatomic) UILabel *tipLabel;
// 保存
@property (weak, nonatomic) UIButton *confirmButton;

@end

@implementation SISSUploadHouseConditionInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"房屋状况说明书";
    
    if (self.houseManualModel) {
        self.uploadContractImageView.imageUrlArr = self.houseManualModel.manual.mutableCopy;
        self.uploadHouseImageView.imageUrlArr = self.houseManualModel.house.mutableCopy;
        self.remarkView.rightTextField.text = self.houseManualModel.otherdesc;
    }
}


// 确定按钮点击事件
- (void)confirmButtonClickAction {

    if (!self.houseManualModel && (!self.uploadContractImageView.images || self.uploadContractImageView.images.count == 0)) {
        [SISSTools showSVWithTitle:@"请选择房屋状况说明书电子档图片"];
        return;
    }
    if (!self.houseManualModel && (!self.uploadHouseImageView.images || self.uploadHouseImageView.images.count == 0)) {
        [SISSTools showSVWithTitle:@"请选择房源实景图片"];
        return;
    }
    NSString *otherdesc = self.remarkView.rightTextField.text ? self.remarkView.rightTextField.text : @"";
    
    if (self.houseManualModel) {
        if (self.houseProgressModel) {
            NSString *ahbpId = self.houseProgressModel ? self.houseProgressModel.ahbpId : @"";
            NSString *ahrId = self.houseProgressModel ? self.houseProgressModel.house.ID : @"";
            NSDictionary *parameters = @{
                                         @"id" : self.houseManualModel.ID,
                                         @"ahbpId" : ahbpId,
                                         @"ahrId" : ahrId,
                                         @"otherdesc" : otherdesc
                                         };
            [kNetworkManager updateHouseStatusManualInfoWithParameters:parameters images:self.uploadContractImageView.images houseImages:self.uploadHouseImageView.images success:^(id responseObject) {
                
                if ([responseObject[@"code"] integerValue] == 200) {
                    [self pushToSuccessViewController];
                }
                
            } failed:^(NSError *error) {
                NSLog(@"%@",error);
            }];
        } else {
            NSString *albpId = self.rentOutProgressModel ? self.rentOutProgressModel.albpId : @"";
            NSString *alrId = self.rentOutProgressModel ? self.rentOutProgressModel.lease.ID : @"";
            NSDictionary *parameters = @{
                                         @"id" : self.houseManualModel.ID,
                                         @"albpId" : albpId,
                                         @"alrId" : alrId,
                                         @"otherdesc" : otherdesc
                                         };
            [kNetworkManager updateRentOutLeaseManualInfoWithParameters:parameters imagesArray:self.uploadContractImageView.images houseImages:self.uploadHouseImageView.images success:^(id responseObject) {
                
                if ([responseObject[@"code"] integerValue] == 200) {
                    [self pushToSuccessViewController];
                }
                
            } failed:^(NSError *error) {
                NSLog(@"%@",error);
            }];
        }
    } else {
        if (self.houseProgressModel) {
            NSString *ahbpId = self.houseProgressModel ? self.houseProgressModel.ahbpId : @"";
            NSString *ahrId = self.houseProgressModel ? self.houseProgressModel.house.ID : @"";
            NSDictionary *parameters = @{
                                         @"ahbpId" : ahbpId,
                                         @"ahrId" : ahrId,
                                         @"otherdesc" : otherdesc
                                         };
            [kNetworkManager insertHouseStatusManualInfoWithParameters:parameters images:self.uploadContractImageView.images houseImages:self.uploadHouseImageView.images success:^(id responseObject) {
                
                if ([responseObject[@"code"] integerValue] == 200) {
                    [self pushToSuccessViewController];
                }
                
            } failed:^(NSError *error) {
                NSLog(@"%@",error);
            }];
        } else {
            NSString *albpId = self.rentOutProgressModel ? self.rentOutProgressModel.albpId : @"";
            NSString *alrId = self.rentOutProgressModel ? self.rentOutProgressModel.lease.ID : @"";
            NSDictionary *parameters = @{
                                         @"albpId" : albpId,
                                         @"alrId" : alrId,
                                         @"otherdesc" : otherdesc
                                         };
            [kNetworkManager insertRentOutLeaseManualInfoWithParameters:parameters imagesArray:self.uploadContractImageView.images houseImages:self.uploadHouseImageView.images success:^(id responseObject) {
                
                if ([responseObject[@"code"] integerValue] == 200) {
                    [self pushToSuccessViewController];
                }
                
            } failed:^(NSError *error) {
                NSLog(@"%@",error);
            }];
        }
    }

}

- (void)pushToSuccessViewController {
    SISSUploadSuccessViewController *vc = [[SISSUploadSuccessViewController alloc] init];
    vc.popBackClassName = @"SISSHouseConditionInfoListViewController";
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    self.containerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    SISSUploadImageView *uploadContractImageView = [[SISSUploadImageView alloc] init];
    uploadContractImageView.titleStr = @"房屋状况说明书电子档图片";
    uploadContractImageView.tag = 0;
    [self.containerView addSubview:uploadContractImageView];
    self.uploadContractImageView = uploadContractImageView;
    
    SISSUploadImageView *uploadHouseImageView = [[SISSUploadImageView alloc] init];
    uploadHouseImageView.titleStr = @"房源实景照片";
    uploadHouseImageView.tag = 1;
    [self.containerView addSubview:uploadHouseImageView];
    self.uploadHouseImageView = uploadHouseImageView;
    
    SISSLabelTextFieldView *remarkView = [[SISSLabelTextFieldView alloc] init];
    remarkView.leftStr = @"备注";
    remarkView.rightPlaceStr = @"请输入备注";
    remarkView.isShowBottomLine = YES;
    [self.containerView addSubview:remarkView];
    self.remarkView = remarkView;
    
    UILabel *tipLabel = [UILabel yyr_labelWithText:@"房屋状况说明由有经纪人实地核实并更新，如上传虚假信息将产生不良行为记录。" fontSize:14 textColor:MAIN_TEXT_COLOR];
    [self.containerView addSubview:tipLabel];
    self.tipLabel = tipLabel;
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmButton setTitle:@"保存" forState:UIControlStateNormal];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmButton setBackgroundColor:MAIN_COLOR];
    [confirmButton addTarget:self action:@selector(confirmButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    confirmButton.layer.cornerRadius = 4;
    confirmButton.layer.masksToBounds = YES;
    [self.containerView addSubview:confirmButton];
    self.confirmButton = confirmButton;
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGSize buttonSize = CGSizeMake(SCREEN_WIDTH * 0.8, 54);
    [self.uploadContractImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.containerView).offset(2 * globelMargin);
        make.left.right.equalTo(weakSelf.containerView);
    }];
    
    [self.uploadHouseImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.uploadContractImageView.mas_bottom).offset(2 * globelMargin);
        make.left.right.equalTo(weakSelf.containerView);
    }];
    
    [self.remarkView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.uploadHouseImageView.mas_bottom).offset(2 * globelMargin);
        make.left.right.equalTo(weakSelf.containerView);
        make.height.mas_equalTo(5.5 * globelMargin);
    }];
    
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.remarkView.mas_bottom).offset(2 * globelMargin);
        make.left.right.equalTo(weakSelf.confirmButton);
    }];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.tipLabel.mas_bottom).offset(2 * globelMargin);
        make.centerX.equalTo(weakSelf.containerView.mas_centerX);
        make.size.mas_equalTo(buttonSize);
        make.bottom.equalTo(weakSelf.containerView).offset(-3 * globelMargin);
    }];
    
}

@end
